package com.kingdee.eas.custom.aoyuan.aytime.basedata;

import java.sql.SQLException;
import java.sql.Time;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.dao.query.IQueryExecutor;
import com.kingdee.bos.dao.query.QueryExecutorFactory;
import com.kingdee.bos.metadata.MetaDataPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.CtrlUnitInfo;
import com.kingdee.eas.basedata.org.FullOrgUnit;
import com.kingdee.eas.basedata.org.FullOrgUnitInfo;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.custom.aoyuan.aytime.TimeCardTypeEnum;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.CardRecordFactory;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.CardRecordInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.ICoreBase;
import com.kingdee.eas.tools.datatask.AbstractDataTransmission;
import com.kingdee.eas.tools.datatask.core.TaskExternalException;
import com.kingdee.eas.tools.datatask.runtime.DataToken;
import com.kingdee.eas.util.client.ExceptionHandler;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.util.TypeConversionUtils;

public class CardRecordDataImport extends AbstractDataTransmission {

	protected ICoreBase getController(Context ctx)
			throws TaskExternalException {
		try {
			return CardRecordFactory.getLocalInstance(ctx);
		} catch (BOSException e) {
			throw new TaskExternalException(e.getMessage());
		}
	}
	
    public void submit(CoreBaseInfo coreBaseInfo, Context ctx)
    throws TaskExternalException
	{
	    try
	    {
	        if(coreBaseInfo == null)
	            return;
	        CardRecordInfo info=(CardRecordInfo)coreBaseInfo;
	        if(info.getEmp()==null)
	        	return;
	        if(info.getBizDate()==null)
	        	return;
	        
	        if(info.getTime1()!=null)
	        {
	        	info.setBizDate(info.getTime1());
	    		info.setBizTime(new Time(info.getTime1().getTime()));
	    		info.setIpAddr("127.0.0.1");
	    		info.setId(null);
	    		submitObj(info,ctx);
	        }
	        
	        if(info.getTime2()!=null)
	        {
	        	info.setBizDate(info.getTime2());
	    		info.setBizTime(new Time(info.getTime2().getTime()));
	    		info.setIpAddr("127.0.0.1");
	    		info.setId(null);
	    		submitObj(info,ctx);
	        }
	        
	        if(info.getTime3()!=null)
	        {
	        	info.setBizDate(info.getTime3());
	    		info.setBizTime(new Time(info.getTime3().getTime()));
	    		info.setIpAddr("127.0.0.1");
	    		info.setId(null);
	    		submitObj(info,ctx);
	        }
	        
	        if(info.getTime4()!=null)
	        {
	        	info.setBizDate(info.getTime4());
	    		info.setBizTime(new Time(info.getTime4().getTime()));
	    		info.setIpAddr("127.0.0.1");
	    		info.setId(null);
	    		submitObj(info,ctx);
	        }
	        
	        if(info.getTime5()!=null)
	        {
	        	info.setBizDate(info.getTime5());
	    		info.setBizTime(new Time(info.getTime5().getTime()));
	    		info.setIpAddr("127.0.0.1");
	    		info.setId(null);
	    		submitObj(info,ctx);
	        }
	        
	        if(info.getTime6()!=null)
	        {
	        	info.setBizDate(info.getTime6());
	    		info.setBizTime(new Time(info.getTime6().getTime()));
	    		info.setIpAddr("127.0.0.1");
	    		info.setId(null);
	    		submitObj(info,ctx);
	        }
	        
	    }
	        catch(Exception ex)
	    {
	         throw new TaskExternalException(ex.getMessage(), ex.getCause());
	    }
	}	
    
    private void submitObj(CoreBaseInfo coreBaseInfo, Context ctx) throws Exception
    {
        if(coreBaseInfo.getId() == null || !getController(ctx).exists(new ObjectUuidPK(coreBaseInfo.getId())))
            getController(ctx).addnew(coreBaseInfo);
         else
        	getController(ctx).update(new ObjectUuidPK(coreBaseInfo.getId()), coreBaseInfo);    	
    }

	public CoreBaseInfo transmit(Hashtable hsData, Context ctx)
			throws TaskExternalException {
		CardRecordInfo info=new CardRecordInfo();
		Object data = null;
		data = getData(hsData, "personNumber");
		try {
		if(data != null && data.toString().trim().length() > 0)
		{
			EntityViewInfo entityViewInfo = new EntityViewInfo();
			FilterInfo filterInfo = new FilterInfo();
			filterInfo.getFilterItems().add(
					new FilterItemInfo("number", data.toString(),
							CompareType.EQUALS));
			entityViewInfo.setFilter(filterInfo);
			PersonCollection personCols=PersonFactory.getLocalInstance(ctx).getPersonCollection(entityViewInfo);
			if(personCols.size()>0)
			{
				info.setEmp(personCols.get(0));
				HROrgUnitInfo hrOrg=getOrgByPerson(ctx,personCols.get(0).getId().toString());
				info.setHROrgUnit(hrOrg);
				CtrlUnitInfo  cu=new CtrlUnitInfo();
				cu.setId(hrOrg.getId());
				info.setCU(cu);
			}
			
			data = getData(hsData, "bizDate");
			if(data != null && data.toString().trim().length() > 0)
			{
				info.setBizDate(TypeConversionUtils.objToDate(data));
				data = getData(hsData, "time1");
				if(data != null && data.toString().trim().length() > 0)
				{
					String[] timeStr=data.toString().split(":");
					if(timeStr.length==2)
					{
			        	Calendar c=Calendar.getInstance();
			    		c.setTime(info.getBizDate());
			    		c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeStr[0]));
			    		c.set(Calendar.MINUTE, Integer.valueOf(timeStr[1]));
			    		c.set(Calendar.SECOND, 0);
						info.setTime1(c.getTime());						
					}
				}
				data = getData(hsData, "time2");
				if(data != null && data.toString().trim().length() > 0)
				{
					String[] timeStr=data.toString().split(":");
					if(timeStr.length==2)
					{
			        	Calendar c=Calendar.getInstance();
			    		c.setTime(info.getBizDate());
			    		c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeStr[0]));
			    		c.set(Calendar.MINUTE, Integer.valueOf(timeStr[1]));
			    		c.set(Calendar.SECOND, 0);
						info.setTime2(c.getTime());						
					}
				}
				data = getData(hsData, "time3");
				if(data != null && data.toString().trim().length() > 0)
				{
					String[] timeStr=data.toString().split(":");
					if(timeStr.length==2)
					{
			        	Calendar c=Calendar.getInstance();
			    		c.setTime(info.getBizDate());
			    		c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeStr[0]));
			    		c.set(Calendar.MINUTE, Integer.valueOf(timeStr[1]));
			    		c.set(Calendar.SECOND, 0);
						info.setTime3(c.getTime());						
					}
				}
				data = getData(hsData, "time4");
				if(data != null && data.toString().trim().length() > 0)
				{
					String[] timeStr=data.toString().split(":");
					if(timeStr.length==2)
					{
			        	Calendar c=Calendar.getInstance();
			    		c.setTime(info.getBizDate());
			    		c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeStr[0]));
			    		c.set(Calendar.MINUTE, Integer.valueOf(timeStr[1]));
			    		c.set(Calendar.SECOND, 0);
						info.setTime4(c.getTime());						
					}
				}
				data = getData(hsData, "time5");
				if(data != null && data.toString().trim().length() > 0)
				{
					String[] timeStr=data.toString().split(":");
					if(timeStr.length==2)
					{
			        	Calendar c=Calendar.getInstance();
			    		c.setTime(info.getBizDate());
			    		c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeStr[0]));
			    		c.set(Calendar.MINUTE, Integer.valueOf(timeStr[1]));
			    		c.set(Calendar.SECOND, 0);
						info.setTime5(c.getTime());						
					}
				}
				
				data = getData(hsData, "time6");
				if(data != null && data.toString().trim().length() > 0)
				{
					String[] timeStr=data.toString().split(":");
					if(timeStr.length==2)
					{
			        	Calendar c=Calendar.getInstance();
			    		c.setTime(info.getBizDate());
			    		c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeStr[0]));
			    		c.set(Calendar.MINUTE, Integer.valueOf(timeStr[1]));
			    		c.set(Calendar.SECOND, 0);
						info.setTime6(c.getTime());						
					}
				}				
			}
			info.setCardType(TimeCardTypeEnum.NORMAL);
		}
		} catch (BOSException e) {
			 ExceptionHandler.handle(e);
		}
		return info;
	}
	
    private Object getData(Hashtable hsData, String fieldName)
    {
       Object data = null;
        if(hsData.get(fieldName) != null)
            data = ((DataToken)hsData.get(fieldName)).data;
        return data;
    }
    
	protected HROrgUnitInfo getOrgByPerson(Context ctx, String id) {
		EntityViewInfo entityViewInfo = new EntityViewInfo();
		FilterInfo filterInfo = new FilterInfo();
		filterInfo.getFilterItems().add(
				new FilterItemInfo("person.id", id,
						CompareType.EQUALS));
		entityViewInfo.setFilter(filterInfo);
		IQueryExecutor iqec = QueryExecutorFactory.getLocalInstance(ctx, new MetaDataPK(    
                "com.kingdee.eas.custom.aoyuan.aytime.basedata.app",    
                "PersonHRQuery"));
		try {
			iqec.setObjectView(entityViewInfo);  
			IRowSet irow = iqec.executeQuery();
			if(irow.size()>0)
			{
				if(irow.next())
				{
					 String hrOrgId=irow.getString("HROrgUnit.id");
					 HROrgUnitInfo info=new HROrgUnitInfo();
					 info.setId(BOSUuid.read(hrOrgId));
					 return info;
				}
			}
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}    

}
